﻿package com.gui.chat {
	import com.effects.Tween;
	import com.events.TweenEvent;
	import com.gui.chat.events.SimpleChatEvent;
	import com.gui.scrollbar.TextFieldScrollBar;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.ui.Keyboard;
	
	/**
	 * ...
	 * @author Ricardo Teixeira
	 */
	public class SimpleChat extends Chat {
		
		private var scroll:TextFieldScrollBar;
		private var scrollMsg:TextFieldScrollBar;
		private var _nomeUsuario:String;
		private var _tutor:Boolean;
		private var minimizado:Boolean;
		private var xIni:Number;
		private var _posicaoPadrao:Point;
		
		public function SimpleChat (nomeUsuario:String = "convidado", tutor:Boolean = false) {
			this.posicaoPadrao = new Point (this.x, this.y);
			this._nomeUsuario = nomeUsuario;
			this._tutor = tutor;
			this.minimizado = false;
			this.addEventListener (Event.ADDED_TO_STAGE, onInit);
		}
		
		private function onInit(e:Event):void {
			removeEventListener(Event.ADDED_TO_STAGE, onInit);
			
			this.usuario.text = this._nomeUsuario;
			
			this.iconeAluno.visible = !(this.iconeTutor.visible = this.tutor);
			
			this.scrollMsg = new TextFieldScrollBar (this.msg, false);
			this.scrollMsg.x = this.msg.x + this.msg.width + 5;
			this.scrollMsg.y = this.msg.y - 3;
			this.scrollMsg.resize (this.msg.height + 6);
			this.addChild (this.scrollMsg);
			
			this.scroll = new TextFieldScrollBar (this.campo, true);
			this.scroll.x = this.campo.x + this.campo.width + 5;
			this.scroll.y = this.campo.y;
			this.addChild (this.scroll);
			
			this.campo.text = "";
			this.msg.text = "";
			this.msg.multiline = false;
			this.msg.wordWrap = true;
			this.usuario.selectable = false;
			
			this.enviar.addEventListener (MouseEvent.CLICK, onClickEnviar);
			this.maximizar.addEventListener (MouseEvent.CLICK, onClickMaximizar);
			this.minimizar.addEventListener (MouseEvent.CLICK, onClickMinimizar);
			this.fundo.addEventListener (MouseEvent.MOUSE_DOWN, onDownFundo);
			this.fundo.addEventListener (MouseEvent.MOUSE_UP, onUpFundo);
			this.fundo.addEventListener (MouseEvent.DOUBLE_CLICK, onFundoDuploClick);
			this.fundo.doubleClickEnabled = true;
			
			this.addEventListener (KeyboardEvent.KEY_DOWN, onDownKey);
		}
		
		private function onFundoDuploClick(e:MouseEvent):void {
			this.x = this.posicaoPadrao.x;
			this.y = this.posicaoPadrao.y;
			this.minimizarChat ();
		}
		
		private function onDownKey(e:KeyboardEvent):void {
		}
		
		public function maximizarChat():void{
			if (this.minimizado) {
				this.xIni = this.x;
				var twn:Tween = new Tween ([220, 25], [0, this.fundo.height], 250);
				twn.addEventListener (TweenEvent.TWEEN_UPDATE, onTweenUpdate);
				twn.addEventListener (TweenEvent.TWEEN_END, onTweenEnd);
				this.minimizado = false;
			}
		}
		
		public function minimizarChat ():void {			
			if (!this.minimizado) {				
				this.xIni = this.x;
				var twn:Tween = new Tween ([0, this.fundo.height], [220, 25], 250);
				twn.addEventListener (TweenEvent.TWEEN_UPDATE, onTweenUpdate);
				twn.addEventListener (TweenEvent.TWEEN_END, onTweenEnd);
				this.minimizado = true;
			}
		}
		
		private function onUpFundo(e:MouseEvent):void {
			this.stopDrag ();
		}
		
		private function onDownFundo(e:MouseEvent):void {
			this.startDrag (false);
		}
		
		private function onClickMaximizar(e:MouseEvent):void {			
			this.maximizarChat ();
		}
		
		private function onClickMinimizar(e:MouseEvent):void {
			this.minimizarChat ();
		}
		
		private function onTweenEnd(e:TweenEvent):void {
			this.scrollRect = new Rectangle (e.value[0], 0, this.fundo.width, e.value[1]);	
		}
		
		private function onTweenUpdate(e:TweenEvent):void {
			this.scrollRect = new Rectangle (e.value[0], 0, this.fundo.width, e.value[1]);			
		}
		
		public function escreverMensagem (msg:String, usuario:String):void {
			var h:Date = new Date ();
			var hora:String = ((h.hours < 10) ? "0" +  h.hours: h.hours) + ":" + ((h.minutes < 10) ? "0" + h.minutes : h.minutes);
			this.campo.appendText ("[" + hora + "][" + usuario + "] " + msg + "\n");
			if (usuario == this.nomeUsuario) {
				this.msg.text = "";
			}
		}
		
		private function onClickEnviar(e:MouseEvent):void {
			//this.escreverMensagem (this.msg.text, this.usu);
			if (this.msg.text.length > 0) {
				var evento:SimpleChatEvent = new SimpleChatEvent (SimpleChatEvent.ENVIA_MENSAGEM, this.msg.text);
				this.dispatchEvent (evento);
			}
		}
		
		public function get nomeUsuario():String { return _nomeUsuario; }
		
		public function set nomeUsuario(value:String):void {
			_nomeUsuario = value;
			this.usuario.text = this._nomeUsuario;
		}
		
		public function get tutor():Boolean { return _tutor; }
		
		public function set tutor(value:Boolean):void {
			_tutor = value;
			this.iconeAluno.visible = !(this.iconeTutor.visible = this.tutor);
		}
		
		public function get posicaoPadrao():Point { return _posicaoPadrao; }
		
		public function set posicaoPadrao(value:Point):void {
			_posicaoPadrao = value;
		}
		
	}
	
}